package com.surfacing.rcp.client;

import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.actions.ContributionItemFactory;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

import com.surfacing.rcp.client.controller.ConnectionWizardAction;
import com.surfacing.rcp.client.controller.LogoutAction;
import com.surfacing.rcp.client.perspectives.SwitchToAdminPerspective;
import com.surfacing.rcp.client.perspectives.SwitchToJournalPerspective;
import com.surfacing.rcp.client.perspectives.SwitchToLogPerspective;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

	// Actions - important to allocate these only in makeActions, and then use
	// them
	// in the fill methods. This ensures that the actions aren't recreated
	// when fillActionBars is called with FILL_PROXY.
	private IWorkbenchAction exitAction;
	private IWorkbenchAction aboutAction;
	private IWorkbenchAction helpAction;
	private IWorkbenchAction preferencesAction;
	private LogoutAction logoutAction;
	private ConnectionWizardAction conWizard;
	private SwitchToAdminPerspective switchToAdmin;
	private SwitchToJournalPerspective switchToJournal;
	private IContributionItem viewList;

	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	protected void makeActions(IWorkbenchWindow window) {
		exitAction = ActionFactory.QUIT.create(window);
		aboutAction = ActionFactory.ABOUT.create(window);
		helpAction = ActionFactory.HELP_CONTENTS.create(window);
		preferencesAction = ActionFactory.PREFERENCES.create(window);
		conWizard = new ConnectionWizardAction(window);
		switchToAdmin = new SwitchToAdminPerspective();
		switchToJournal = new SwitchToJournalPerspective();
		viewList = ContributionItemFactory.VIEWS_SHORTLIST.create(window);
		logoutAction = new LogoutAction(window);
		register(aboutAction);
		register(exitAction);
		register(helpAction);
		register(preferencesAction);
		register(logoutAction);
	}

	protected void fillMenuBar(IMenuManager menuBar) {
		MenuManager fileMenu = new MenuManager("&File",
				IWorkbenchActionConstants.M_FILE);
		fileMenu.add(logoutAction);
		fileMenu.add(new Separator());
		fileMenu.add(exitAction);

		MenuManager adminMenu = new MenuManager("&Administrator",
				IWorkbenchActionConstants.M_WINDOW);
		adminMenu.add(switchToAdmin);
		adminMenu.add(viewList);

		MenuManager windowMenu = new MenuManager("&Window",
				IWorkbenchActionConstants.M_WINDOW);
		windowMenu.add(adminMenu);
		windowMenu.add(new Separator());
		windowMenu.add(switchToJournal);

		MenuManager helpMenu = new MenuManager("&Help");
		helpMenu.add(aboutAction);
		helpMenu.add(conWizard);

		menuBar.add(fileMenu);
		menuBar.add(windowMenu);
		menuBar.add(helpMenu);
		
	}

	/**
	 * Called to fill the cool bar with the main toolbars for the window.
	 */
	@Override
	protected void fillCoolBar(ICoolBarManager coolBar) {
		IToolBarManager toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
		coolBar.add(new ToolBarContributionItem(toolbar, "main"));
	}
}